﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace EzyChoice
{
    [Serializable()]
    public class Questionnaire : TreeNode, ISerializable
    {
        protected string guid;
        public string ID
        {
            get
            {
                return guid;
            }
        }

        public Criteria parrent = new Criteria();

        public double weight;

        protected string name;

        public string Label
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }



        public Questionnaire()
        {
            this.guid = Guid.NewGuid().ToString();
        }

        public Questionnaire(string name, Criteria parrent)
        {
            this.guid = Guid.NewGuid().ToString();
            this.parrent = parrent;
            this.name = name;
        }

        protected Questionnaire(SerializationInfo info, StreamingContext context)
        {
            this.name = (string)info.GetValue("name", typeof(string));
            this.weight = (double)info.GetValue("weight", typeof(double));
            this.guid = (string)info.GetValue("guid", typeof(string));
            this.parrent = (Criteria)info.GetValue("parrent", typeof(Criteria));

            this.Text = this.ToString();
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("name", this.name);
            info.AddValue("weight", this.weight);
            info.AddValue("guid", this.guid);
            info.AddValue("parrent", this.parrent);
        }

        public override string ToString()
        {
            return name + " (" + weight + ")";
        }

        public double GetNormalizedWeight()
        {
            double norm = 0.0;
            if (parrent != null)
            {
                double total = 0.0;

                foreach (Questionnaire q in parrent.Choices)
                {
                    total += q.weight;
                }

             
                norm = total > 0 ? Math.Round(this.weight / total, 3) : 0;
            }
            else
            {
                norm = weight;
            }

            return norm;
        }

    }
}
